
import http from '../../../service/http.js'
import shouquan from '../../../class/login.js'
import tips from '../../../class/Utils/Tips'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    menustatus: false,
    dingtouming: 1,
    dingweizhi: 0,
    headstate: false,
    fenleilist: [],
    fenleiid: 100,
    juanlist: [
      { id: '1', zhekou: '8', xianling: '1', shiyong: '指定商品可用，无使用门槛', zhuangtai: '0', },
      { id: '2', zhekou: '2', xianling: '1', shiyong: '指定商品可用，无使用门槛', zhuangtai: '1', },
    ],
    prolist: [],
    searchs: 1,
    movies: [],
    // banner 图片切换
    currentNavtab: 0,//当前第几个swiper
    autoplay: true,
    interval: 5000,
    duration: 500,
    scrollWidth: 0,
    imgheights: [],
    imgheightsmid: [],
    tuino: 0,
    remeno: 0,
    noslider: false
  },
  onLoad: function (options) {


    var that = this;
    //获取顶部导航节点 begin
    const query = wx.createSelectorQuery()
    query.select('.el-shoptop-menu').boundingClientRect(function (rect) {
      that.setData({
        menuheight: rect.height
      })
    })
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      res[0].top       // #the-id节点的上边界坐标
      that.setData({
        menutop: res[0].top
      })

    })
    //获取顶部导航节点 begin

    //获取顶部区域节点 begin
    const queryb = wx.createSelectorQuery()
    queryb.select('.el-head').boundingClientRect(function (rect) {
      console.log(rect)
      that.setData({
        headtop: rect.height
      })
    })
    queryb.selectViewport().scrollOffset()
    queryb.exec(function (res) {
      res[0].top       // #the-id节点的上边界坐标
      res[1].scrollTop // 显示区域的竖直滚动位置
    })
    //获取顶部区域节点 begin

    //获取分类导航区域节点 begin
    const queryc = wx.createSelectorQuery()
    queryc.select('.el-shoptop-menu-li').boundingClientRect(function (recb) {
      console.log(recb)
      recb.id      // 节点的ID
      recb.dataset // 节点的dataset
      recb.left    // 节点的左边界坐标
      recb.right   // 节点的右边界坐标
      recb.top     // 节点的上边界坐标
      recb.bottom  // 节点的下边界坐标
      recb.width   // 节点的宽度
      recb.height  // 节点的高度
      that.setData({
        menuliwidth: recb.width
      })
    })
    queryc.selectViewport().scrollOffset()
    queryc.exec(function (res) {
      res[0].top       // #the-id节点的上边界坐标
      res[1].scrollTop // 显示区域的竖直滚动位置
    })
    //获取分类导航区域节点 begin





    if (wx.getStorageSync('user')) {
      that.setData({
        shouquan: 0
      })
    } else {
      that.setData({
        shouquan: 1
      })
    }


    http.req('/Wxpro/getslider', {}, function (res) {

      if (res.errcode == 0) {
        that.setData({
          shang: res.data
        })
      } else {
        that.setData({
          noslider: true
        })
      }
    });

    http.req('/Wxpro/type_list', {}, function (res) {

      if (res.code == 1) {
        that.setData({
          fenleilist: res.typelist,
          prolist: res.prolist
        })
      } else {
        that.setData({
          tuino: 1
        })
      }
    });



    // http.req('/Wxpro/sytui4', {}, function (res) {
    //   if (res.code == '0') {
    //     that.setData({
    //       tui: res.list
    //     })
    //     //console.log(that.data.tui)
    //   } else {
    //     that.setData({
    //       tuino: 1
    //     })
    //   }
    // });


    wx.getSystemInfo({
      success: function (res) {
        //获取屏幕的宽度并保存
        that.setData({
          scrollWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  /*** 预览图片****/
  previewImage: function (e) {
    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current,// 当前显示图片的http链接
      urls: this.data.bannerlist // 需要预览的图片http链接列表
    })
  },
  //等比缩放图片并保存
  imageLoad: function (e) {
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;
    //console.log(imgwidth, imgheight);
    //计算的高度值

    var viewHeight = parseInt(this.data.scrollWidth) / ratio;
    var imgheight = viewHeight.toFixed(0);
    var imgheightarray = this.data.imgheights;
    //把每一张图片的高度记录到数组里
    imgheightarray.push(imgheight);

    this.setData({
      imgheights: imgheightarray,
    });
  },
  onShow: function () {

  },

  //监听 page 滚动条位置
  onPageScroll: function (res) {
    var that = this
    var topscroll = res.scrollTop
    var menutop = that.data.menutop
    var headtop = that.data.headtop
    var dingtouming = 1 - (topscroll / 100)

    that.setData({
      scrollTop: res.scrollTop
    })

    if (topscroll > headtop) {
      that.setData({
        headstate: true,
      })
    } else {
      that.setData({
        headstate: false,
      })
    }

    if (topscroll == 0) {
      that.setData({
        dingweizhi: 0,
        dingtouming: 1,
      })
    } else if (topscroll < 0) {
      that.setData({
        dingweizhi: 0,
        dingtouming: 1,
      })
    } else {
      that.setData({
        dingweizhi: -topscroll,
        dingtouming: dingtouming,
      })
    }

    //导航变成 绝对定位顶部 begin
    if (topscroll > menutop) {
      that.setData({
        menustatus: true,
      })
    } else {
      that.setData({
        menustatus: false,
      })
    }
    //导航变成 绝对定位顶部 end
    console.log(topscroll)
    console.log(menutop)
  },
  //领取优惠券
  lingjuan: function (e) {
    var that = this
    var thisindex = e.currentTarget.dataset.index
    console.log(thisindex)
    var datalist = that.data.juanlist
    for (var i = 0; i < datalist.length; i++) {
      datalist[i].zhuangtai = 1
    }
    that.setData({
      juanlist: datalist
    })
  },
  //选择分类产品
  changfenlei: function (e) {
    var that = this
    var thisindex = e.currentTarget.dataset.index
    var typeid = e.currentTarget.dataset.typeid
    var menutop = that.data.menutop
    var menuheight = that.data.menuheight
    var menuweizhi = menutop - menuheight
    var scrollTop = that.data.scrollTop

    if (scrollTop > menutop) {
      wx.pageScrollTo({
        scrollTop: menuweizhi,
        duration: 0
      })
    }



    if (thisindex == '100') {
      that.setData({
        fenleiid: 100
      })
    } else {
      var thisid = that.data.fenleilist[thisindex].id
      that.setData({
        fenleiid: thisid
      })
    }
    that.autoScrollLeftNav(thisindex);

    if(typeid!=0){
      var typeparam = { typeid: typeid }
    }else{
      var typeparam = {}
    }
    http.req('/Wxpro/type_list', typeparam, function (res) {

      if (res.code == 1) {
        that.setData({
          prolist: res.prolist,
          tuino: 0
        })
      } else {
        that.setData({
          tuino: 1
        })
      }
    });
    


  },
  autoScrollLeftNav: function (thisindex) {
    var that = this
    var menuwwidth = that.data.menuliwidth

    if (thisindex < 0 || thisindex == 100) {
      that.data.scrollLeft = 0;
    } else {
      that.data.scrollLeft = thisindex * menuwwidth;
    }
    
    
    that.setData({
      scrollLeft: that.data.scrollLeft
    })
  },
  //分享
  onShareAppMessage: function () {
    var that = this
    return {
      title: '浙江网盛生意宝股份有限公司 - Cloud Ai',
      path: '',
      imageUrl: "",
      success: (res) => {
        console.log("转发成功", res);
      },
    }
  },
  //去会员中心
  gotomain: function () {
    var that = this
    tips.goto('/pages/shop/main/index/index')
  },


})